package com.lostandfound.controller;

import com.lostandfound.pojo.*;
import com.lostandfound.pojo.dto.PublishDto;
import com.lostandfound.pojo.vo.PublishVo;
import com.lostandfound.service.FindService;
import com.lostandfound.service.PublishService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;

@RestController
@Slf4j
@RequestMapping("/find")
public class FindController {

    @Autowired
    private FindService findService;

    @Autowired
    private PublishService publishService;

    /**
     * 查询寻物表
     * @param queryPageBean
     * @return
     */
    @PostMapping
    public Result page(@RequestBody QueryPageBean queryPageBean) {
        log.info("page:{}",queryPageBean.getCurrentPage());
        log.info("pageSize:{}",queryPageBean.getPageSize());
        log.info("name:{}",queryPageBean.getName());
        log.info("begin:{}",queryPageBean.getBegin());
        log.info("end:{}",queryPageBean.getEnd());
        PageBean pageBean = findService.page(queryPageBean.getCurrentPage(), queryPageBean.getPageSize(), queryPageBean.getName(), queryPageBean.getBegin(), queryPageBean.getEnd());
        return Result.success(pageBean);

    }

    /**
     * 根据id查询
     * @param id
     * @return
     */
    @GetMapping("/findById")
    public PublishVo findById(Integer id){
        return findService.findById(id);
    }

    /**
     * 发布
     * @param publishDto
     * @return
     */
    @PostMapping("/add")
    public Result add(@RequestBody PublishDto publishDto) {
        /*RecommendStaff recommendStaff = new RecommendStaff();
        BeanUtils.copyProperties(empDto,recommendStaff);
        StaffEmp staffEmp = new StaffEmp();
        BeanUtils.copyProperties(empDto,staffEmp);
        log.info("======================staffEmp:{}",staffEmp);
        empAdminService.add(recommendStaff,empDto);*/
        String contacts = publishDto.getCategory();
        if (contacts.equals("寻物启事")){
            publishDto.setStamp(1);
            FindAny findAny = new FindAny();
            BeanUtils.copyProperties(publishDto,findAny);
            log.info("===============publish:{}", publishDto);
            findAny.setReleaseTime(LocalDateTime.now());
            findAny.setLostTime(publishDto.getLocalDate());
            findAny.setLostPlace(publishDto.getLocalPlace());
            publishService.add(findAny);
            return Result.success("添加成功",null);
        }else if (contacts.equals("失物招领")){
            publishDto.setStamp(0);
            Recruit recruit = new Recruit();
            BeanUtils.copyProperties(publishDto,recruit);
            recruit.setReleaseTime(LocalDateTime.now());
            recruit.setCollectTime(publishDto.getLocalDate());
            recruit.setCollectPlace(publishDto.getLocalPlace());
            log.info("===============recruit:{}",recruit);
            publishService.addRecruit(recruit);
            return Result.success("添加成功",null);
        }

        return Result.success("请认真填写发布类别!",null);

    }

    /**
     * 编辑物品信息
     * @param
     * @return
     */
    @RequestMapping("/update")
    public Result update(@RequestBody PublishDto publishDto) {
        findService.update(publishDto);
        return Result.success("修改成功",1);
    }

    /**
     * 删除物品
     */
    @PostMapping("/delete")
    public Result delete(Integer id) {
        findService.delete(id);
        return Result.success();
    }



}
